package com.hghivln.erpwarehouse.business.controller;


import com.hghivln.erpwarehouse.business.entity.Goods;
import com.hghivln.erpwarehouse.business.service.GoodsService;
import com.hghivln.erpwarehouse.business.vo.GoodsVo;
import com.hghivln.erpwarehouse.system.common.lang.DataGridlView;
import com.hghivln.erpwarehouse.system.common.lang.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

/**
 * <p>
 *  商品管理接口
 * </p>
 *
 * @author hghivln
 * @since 2021-07-10
 */
@RestController
@RequestMapping("api/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("loadAllGoods")
    public Object loadAllGoods(GoodsVo goodsVo) {
        return goodsService.QueryAllGoods(goodsVo);
    }

    @RequiresPermissions("goods:add")
    @RequestMapping("addGoods")
    public Result addGoods(Goods goods) {
        try {
            goodsService.addGoods(goods);
            return Result.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }

    @RequiresPermissions("goods:update")
    @RequestMapping("updateGoods")
    public Result updateGoods(Goods goods) {
        try {
            goodsService.updateGoods(goods);
            return Result.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    @RequiresPermissions("goods:delete")
    @RequestMapping("deleteGoods")
    public Result deleteGoods(Integer id) {
        try {
            goodsService.removeById(id);
            return Result.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.DELETE_ERROR;
        }
    }

    @RequiresPermissions("goods:batchdelete")
    @RequestMapping("batchDeleteGoods")
    public Result batchDeleteGoods(Integer[] ids){
        try {
            Collection<Serializable> idList = new ArrayList<>(Arrays.asList(ids));
            goodsService.removeByIds(idList);
            return Result.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.DELETE_ERROR;
        }
    }

    @RequestMapping("getAllAvailableGoods")
    public Object getAllAvailableGoods() {
        return goodsService.getAllAvailableGoods();
    }

    @RequestMapping("getGoodsByProviderId")
    public Object getGoodsByProviderId(Integer id) {
        return goodsService.getGoodsByProviderId(id);
    }

    @RequestMapping("getGoodsByGoodId")
    public DataGridlView getGoodsByGoodId(Integer goodsid) {
        Goods goods = goodsService.getById(goodsid);
        return new DataGridlView(goods);
    }

}
